{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Generating Bug Free Leetcode Solutions\n",
    "\n",
    "!!! note\n",
    "    To download this tutorial as a Jupyter notebook, click [here](https://github.com/guardrails-ai/guardrails/blob/main/docs/examples/bug_free_python_code.ipynb).\n",
    "\n",
    "In this example, we want to solve String Maniuplation leetcode problems such that the code is bug free.\n",
    "\n",
    "We make the assumption that:\n",
    "\n",
    "1. We don't need any external libraries that are not already installed in the environment.\n",
    "2. We are able to execute the code in the environment.\n",
    "\n",
    "## Objective\n",
    "\n",
    "We want to generate bug-free code for solving leetcode problems. In this example, we don't account for semantic bugs, only for syntactic bugs.\n",
    "\n",
    "In short, we want to make sure that the code can be executed without any errors.\n",
    "\n",
    "\n",
    "## Step 1: Generating `RAIL` Spec\n",
    "\n",
    "Ordinarily, we could create a separate `RAIL` spec in a file. However, for the sake of this example, we will generate the `RAIL` spec in the notebook as a string.  We will also show the same RAIL spec in a code-first format using a Pydantic model."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "XML option:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "rail_str = \"\"\"\n",
    "<rail version=\"0.1\">\n",
    "\n",
    "<output>\n",
    "    <pythoncode\n",
    "        name=\"python_code\"\n",
    "        format=\"bug-free-python\"\n",
    "        on-fail-bug-free-python=\"reask\"\n",
    "    />\n",
    "</output>\n",
    "\n",
    "\n",
    "<prompt>\n",
    "Given the following high level leetcode problem description, write a short Python code snippet that solves the problem.\n",
    "\n",
    "Problem Description:\n",
    "${leetcode_problem}\n",
    "\n",
    "${gr.complete_json_suffix}</prompt>\n",
    "\n",
    "</rail>\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Pydantic model option:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pydantic import BaseModel, Field\n",
    "from guardrails.validators import BugFreePython\n",
    "\n",
    "prompt = \"\"\"\n",
    "Given the following high level leetcode problem description, write a short Python code snippet that solves the problem.\n",
    "\n",
    "Problem Description:\n",
    "${leetcode_problem}\n",
    "\n",
    "${gr.complete_json_suffix}\"\"\"\n",
    "\n",
    "class BugFreePythonCode(BaseModel):\n",
    "    python_code: str = Field(validators=[BugFreePython(on_fail=\"reask\")])\n",
    "\n",
    "    class Config:\n",
    "        arbitrary_types_allowed = True"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 2: Create a `Guard` object with the RAIL Spec\n",
    "\n",
    "We create a `gd.Guard` object that will check, validate and correct the generated code. This object:\n",
    "\n",
    "1. Enforces the quality criteria specified in the RAIL spec (i.e. bug free code).\n",
    "2. Takes corrective action when the quality criteria are not met (i.e. reasking the LLM).\n",
    "3. Compiles the schema and type info from the RAIL spec and adds it to the prompt."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "import guardrails as gd\n",
    "\n",
    "from rich import print"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "From XML:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "guard = gd.Guard.from_rail_string(rail_str)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Or from the pydantic model:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "guard = gd.Guard.from_pydantic(output_class=BugFreePythonCode, prompt=prompt)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `Guard` object compiles the output schema and adds it to the prompt. We can see the final prompt below:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">\n",
       "Given the following high level leetcode problem description, write a short Python code snippet that solves the \n",
       "problem.\n",
       "\n",
       "Problem Description:\n",
       "$<span style=\"font-weight: bold\">{</span>leetcode_problem<span style=\"font-weight: bold\">}</span>\n",
       "\n",
       "\n",
       "Given below is XML that describes the information to extract from this document and the tags to extract it into.\n",
       "\n",
       "<span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; text-decoration-color: #ff00ff; font-weight: bold\">output</span><span style=\"color: #000000; text-decoration-color: #000000\">&gt;</span>\n",
       "<span style=\"color: #000000; text-decoration-color: #000000\">    &lt;string </span><span style=\"color: #808000; text-decoration-color: #808000\">name</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #008000; text-decoration-color: #008000\">\"python_code\"</span><span style=\"color: #000000; text-decoration-color: #000000\"> </span><span style=\"color: #808000; text-decoration-color: #808000\">format</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #008000; text-decoration-color: #008000\">\"bug-free-python\"</span><span style=\"color: #800080; text-decoration-color: #800080\">/</span><span style=\"color: #000000; text-decoration-color: #000000\">&gt;</span>\n",
       "<span style=\"color: #000000; text-decoration-color: #000000\">&lt;</span><span style=\"color: #800080; text-decoration-color: #800080\">/</span><span style=\"color: #ff00ff; text-decoration-color: #ff00ff\">output</span><span style=\"color: #000000; text-decoration-color: #000000\">&gt;</span>\n",
       "\n",
       "\n",
       "<span style=\"color: #000000; text-decoration-color: #000000\">ONLY return a valid JSON object </span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">(</span><span style=\"color: #000000; text-decoration-color: #000000\">no other text is necessary</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">)</span><span style=\"color: #000000; text-decoration-color: #000000\">, where the key of the field in JSON is the `name` </span>\n",
       "<span style=\"color: #000000; text-decoration-color: #000000\">attribute of the corresponding XML, and the value is of the type specified by the corresponding XML's tag. The JSON</span>\n",
       "<span style=\"color: #000000; text-decoration-color: #000000\">MUST conform to the XML format, including any types and format requests e.g. requests for lists, objects and </span>\n",
       "<span style=\"color: #000000; text-decoration-color: #000000\">specific types. Be correct and concise. If you are unsure anywhere, enter `null`.</span>\n",
       "\n",
       "<span style=\"color: #000000; text-decoration-color: #000000\">Here are examples of simple </span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">(</span><span style=\"color: #000000; text-decoration-color: #000000\">XML, JSON</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">)</span><span style=\"color: #000000; text-decoration-color: #000000\"> pairs that show the expected behavior:</span>\n",
       "<span style=\"color: #000000; text-decoration-color: #000000\">- `&lt;string </span><span style=\"color: #808000; text-decoration-color: #808000\">name</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #008000; text-decoration-color: #008000\">'foo'</span><span style=\"color: #000000; text-decoration-color: #000000\"> </span><span style=\"color: #808000; text-decoration-color: #808000\">format</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #008000; text-decoration-color: #008000\">'two-words lower-case'</span><span style=\"color: #000000; text-decoration-color: #000000\"> </span><span style=\"color: #800080; text-decoration-color: #800080\">/</span><span style=\"color: #000000; text-decoration-color: #000000\">&gt;` =&gt; `</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">{</span><span style=\"color: #008000; text-decoration-color: #008000\">'foo'</span><span style=\"color: #000000; text-decoration-color: #000000\">: </span><span style=\"color: #008000; text-decoration-color: #008000\">'example one'</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">}</span><span style=\"color: #000000; text-decoration-color: #000000\">`</span>\n",
       "<span style=\"color: #000000; text-decoration-color: #000000\">- `&lt;list </span><span style=\"color: #808000; text-decoration-color: #808000\">name</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #008000; text-decoration-color: #008000\">'bar'</span><span style=\"color: #000000; text-decoration-color: #000000\">&gt;&lt;string </span><span style=\"color: #808000; text-decoration-color: #808000\">format</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #008000; text-decoration-color: #008000\">'upper-case'</span><span style=\"color: #000000; text-decoration-color: #000000\"> </span><span style=\"color: #800080; text-decoration-color: #800080\">/</span><span style=\"color: #000000; text-decoration-color: #000000\">&gt;&lt;</span><span style=\"color: #800080; text-decoration-color: #800080\">/</span><span style=\"color: #ff00ff; text-decoration-color: #ff00ff\">list</span><span style=\"color: #000000; text-decoration-color: #000000\">&gt;` =&gt; `</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">{</span><span style=\"color: #008000; text-decoration-color: #008000\">\"bar\"</span><span style=\"color: #000000; text-decoration-color: #000000\">: </span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">[</span><span style=\"color: #008000; text-decoration-color: #008000\">'STRING ONE'</span><span style=\"color: #000000; text-decoration-color: #000000\">, </span><span style=\"color: #008000; text-decoration-color: #008000\">'STRING TWO'</span><span style=\"color: #000000; text-decoration-color: #000000\">, etc.</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">]}</span><span style=\"color: #000000; text-decoration-color: #000000\">`</span>\n",
       "<span style=\"color: #000000; text-decoration-color: #000000\">- `&lt;object </span><span style=\"color: #808000; text-decoration-color: #808000\">name</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #008000; text-decoration-color: #008000\">'baz'</span><span style=\"color: #000000; text-decoration-color: #000000\">&gt;&lt;string </span><span style=\"color: #808000; text-decoration-color: #808000\">name</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #008000; text-decoration-color: #008000\">\"foo\"</span><span style=\"color: #000000; text-decoration-color: #000000\"> </span><span style=\"color: #808000; text-decoration-color: #808000\">format</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #008000; text-decoration-color: #008000\">\"capitalize two-words\"</span><span style=\"color: #000000; text-decoration-color: #000000\"> </span><span style=\"color: #800080; text-decoration-color: #800080\">/</span><span style=\"color: #000000; text-decoration-color: #000000\">&gt;&lt;integer </span><span style=\"color: #808000; text-decoration-color: #808000\">name</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #008000; text-decoration-color: #008000\">\"index\"</span><span style=\"color: #000000; text-decoration-color: #000000\"> </span><span style=\"color: #808000; text-decoration-color: #808000\">format</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #008000; text-decoration-color: #008000\">\"1-indexed\"</span><span style=\"color: #000000; text-decoration-color: #000000\"> </span>\n",
       "<span style=\"color: #800080; text-decoration-color: #800080\">/</span><span style=\"color: #000000; text-decoration-color: #000000\">&gt;&lt;</span><span style=\"color: #800080; text-decoration-color: #800080\">/</span><span style=\"color: #ff00ff; text-decoration-color: #ff00ff\">object</span><span style=\"color: #000000; text-decoration-color: #000000\">&gt;` =</span><span style=\"font-weight: bold\">&gt;</span> `<span style=\"font-weight: bold\">{</span><span style=\"color: #008000; text-decoration-color: #008000\">'baz'</span>: <span style=\"font-weight: bold\">{</span><span style=\"color: #008000; text-decoration-color: #008000\">'foo'</span>: <span style=\"color: #008000; text-decoration-color: #008000\">'Some String'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'index'</span>: <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span><span style=\"font-weight: bold\">}}</span>`\n",
       "\n",
       "</pre>\n"
      ],
      "text/plain": [
       "\n",
       "Given the following high level leetcode problem description, write a short Python code snippet that solves the \n",
       "problem.\n",
       "\n",
       "Problem Description:\n",
       "$\u001b[1m{\u001b[0mleetcode_problem\u001b[1m}\u001b[0m\n",
       "\n",
       "\n",
       "Given below is XML that describes the information to extract from this document and the tags to extract it into.\n",
       "\n",
       "\u001b[1m<\u001b[0m\u001b[1;95moutput\u001b[0m\u001b[39m>\u001b[0m\n",
       "\u001b[39m    <string \u001b[0m\u001b[33mname\u001b[0m\u001b[39m=\u001b[0m\u001b[32m\"python_code\"\u001b[0m\u001b[39m \u001b[0m\u001b[33mformat\u001b[0m\u001b[39m=\u001b[0m\u001b[32m\"bug\u001b[0m\u001b[32m-free-python\"\u001b[0m\u001b[35m/\u001b[0m\u001b[39m>\u001b[0m\n",
       "\u001b[39m<\u001b[0m\u001b[35m/\u001b[0m\u001b[95moutput\u001b[0m\u001b[39m>\u001b[0m\n",
       "\n",
       "\n",
       "\u001b[39mONLY return a valid JSON object \u001b[0m\u001b[1;39m(\u001b[0m\u001b[39mno other text is necessary\u001b[0m\u001b[1;39m)\u001b[0m\u001b[39m, where the key of the field in JSON is the `name` \u001b[0m\n",
       "\u001b[39mattribute of the corresponding XML, and the value is of the type specified by the corresponding XML's tag. The JSON\u001b[0m\n",
       "\u001b[39mMUST conform to the XML format, including any types and format requests e.g. requests for lists, objects and \u001b[0m\n",
       "\u001b[39mspecific types. Be correct and concise. If you are unsure anywhere, enter `null`.\u001b[0m\n",
       "\n",
       "\u001b[39mHere are examples of simple \u001b[0m\u001b[1;39m(\u001b[0m\u001b[39mXML, JSON\u001b[0m\u001b[1;39m)\u001b[0m\u001b[39m pairs that show the expected behavior:\u001b[0m\n",
       "\u001b[39m- `<string \u001b[0m\u001b[33mname\u001b[0m\u001b[39m=\u001b[0m\u001b[32m'foo'\u001b[0m\u001b[39m \u001b[0m\u001b[33mformat\u001b[0m\u001b[39m=\u001b[0m\u001b[32m'two-words lower-case'\u001b[0m\u001b[39m \u001b[0m\u001b[35m/\u001b[0m\u001b[39m>` => `\u001b[0m\u001b[1;39m{\u001b[0m\u001b[32m'foo'\u001b[0m\u001b[39m: \u001b[0m\u001b[32m'example one'\u001b[0m\u001b[1;39m}\u001b[0m\u001b[39m`\u001b[0m\n",
       "\u001b[39m- `<list \u001b[0m\u001b[33mname\u001b[0m\u001b[39m=\u001b[0m\u001b[32m'bar'\u001b[0m\u001b[39m><string \u001b[0m\u001b[33mformat\u001b[0m\u001b[39m=\u001b[0m\u001b[32m'upper-case'\u001b[0m\u001b[39m \u001b[0m\u001b[35m/\u001b[0m\u001b[39m><\u001b[0m\u001b[35m/\u001b[0m\u001b[95mlist\u001b[0m\u001b[39m>` => `\u001b[0m\u001b[1;39m{\u001b[0m\u001b[32m\"bar\"\u001b[0m\u001b[39m: \u001b[0m\u001b[1;39m[\u001b[0m\u001b[32m'STRING ONE'\u001b[0m\u001b[39m, \u001b[0m\u001b[32m'STRING TWO'\u001b[0m\u001b[39m, etc.\u001b[0m\u001b[1;39m]\u001b[0m\u001b[1;39m}\u001b[0m\u001b[39m`\u001b[0m\n",
       "\u001b[39m- `<object \u001b[0m\u001b[33mname\u001b[0m\u001b[39m=\u001b[0m\u001b[32m'baz'\u001b[0m\u001b[39m><string \u001b[0m\u001b[33mname\u001b[0m\u001b[39m=\u001b[0m\u001b[32m\"foo\"\u001b[0m\u001b[39m \u001b[0m\u001b[33mformat\u001b[0m\u001b[39m=\u001b[0m\u001b[32m\"capitalize\u001b[0m\u001b[32m two-words\"\u001b[0m\u001b[39m \u001b[0m\u001b[35m/\u001b[0m\u001b[39m><integer \u001b[0m\u001b[33mname\u001b[0m\u001b[39m=\u001b[0m\u001b[32m\"index\"\u001b[0m\u001b[39m \u001b[0m\u001b[33mformat\u001b[0m\u001b[39m=\u001b[0m\u001b[32m\"1\u001b[0m\u001b[32m-indexed\"\u001b[0m\u001b[39m \u001b[0m\n",
       "\u001b[35m/\u001b[0m\u001b[39m><\u001b[0m\u001b[35m/\u001b[0m\u001b[95mobject\u001b[0m\u001b[39m>` =\u001b[0m\u001b[1m>\u001b[0m `\u001b[1m{\u001b[0m\u001b[32m'baz'\u001b[0m: \u001b[1m{\u001b[0m\u001b[32m'foo'\u001b[0m: \u001b[32m'Some String'\u001b[0m, \u001b[32m'index'\u001b[0m: \u001b[1;36m1\u001b[0m\u001b[1m}\u001b[0m\u001b[1m}\u001b[0m`\n",
       "\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "print(guard.base_prompt)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 3: Wrap the LLM API call with `Guard`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "HTTP Request: POST https://api.openai.com/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
      "HTTP Request: POST https://api.openai.com/v1/chat/completions \"HTTP/1.1 200 OK\"\n"
     ]
    }
   ],
   "source": [
    "import openai\n",
    "\n",
    "leetcode_problem = \"\"\"\n",
    "Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.\n",
    "\"\"\"\n",
    "\n",
    "response = guard(\n",
    "    openai.chat.completions.create,\n",
    "    prompt_params={\"leetcode_problem\": leetcode_problem},\n",
    "    max_tokens=2048,\n",
    "    temperature=0,\n",
    ")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Running the cell above returns:\n",
    "1. The raw LLM text output as a single string.\n",
    "2. A dictionary where the key is `python_code` and the value is the generated code."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\">{</span>\n",
       "    <span style=\"color: #008000; text-decoration-color: #008000\">'python_code'</span>: <span style=\"color: #008000; text-decoration-color: #008000\">'def longestPalindrome(s):\\n    if len(s) &lt; 2:\\n        return s\\n    start = 0\\n    max_len = </span>\n",
       "<span style=\"color: #008000; text-decoration-color: #008000\">1\\n    for i in range(len(s)):\\n        if i - max_len &gt;= 1 and s[i-max_len-1:i+1] == s[i-max_len-1:i+1][::-1]:\\n  </span>\n",
       "<span style=\"color: #008000; text-decoration-color: #008000\">start = i - max_len - 1\\n            max_len += 2\\n            continue\\n        if i - max_len &gt;= 0 and </span>\n",
       "<span style=\"color: #008000; text-decoration-color: #008000\">s[i-max_len:i+1] == s[i-max_len:i+1][::-1]:\\n            start = i - max_len\\n            max_len += 1\\n    return </span>\n",
       "<span style=\"color: #008000; text-decoration-color: #008000\">s[start:start+max_len]'</span>\n",
       "<span style=\"font-weight: bold\">}</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "\u001b[1m{\u001b[0m\n",
       "    \u001b[32m'python_code'\u001b[0m: \u001b[32m'def longestPalindrome\u001b[0m\u001b[32m(\u001b[0m\u001b[32ms\u001b[0m\u001b[32m)\u001b[0m\u001b[32m:\\n    if len\u001b[0m\u001b[32m(\u001b[0m\u001b[32ms\u001b[0m\u001b[32m)\u001b[0m\u001b[32m \u001b[0m\u001b[32m<\u001b[0m\u001b[32m 2:\\n        return s\\n    start = 0\\n    max_len = \u001b[0m\n",
       "\u001b[32m1\\n    for i in range\u001b[0m\u001b[32m(\u001b[0m\u001b[32mlen\u001b[0m\u001b[32m(\u001b[0m\u001b[32ms\u001b[0m\u001b[32m)\u001b[0m\u001b[32m)\u001b[0m\u001b[32m:\\n        if i - max_len >= 1 and s\u001b[0m\u001b[32m[\u001b[0m\u001b[32mi-max_len-1:i+1\u001b[0m\u001b[32m]\u001b[0m\u001b[32m == s\u001b[0m\u001b[32m[\u001b[0m\u001b[32mi-max_len-1:i+1\u001b[0m\u001b[32m]\u001b[0m\u001b[32m[\u001b[0m\u001b[32m::-1\u001b[0m\u001b[32m]\u001b[0m\u001b[32m:\\n  \u001b[0m\n",
       "\u001b[32mstart = i - max_len - 1\\n            max_len += 2\\n            continue\\n        if i - max_len \u001b[0m\u001b[32m>\u001b[0m\u001b[32m= 0 and \u001b[0m\n",
       "\u001b[32ms\u001b[0m\u001b[32m[\u001b[0m\u001b[32mi-max_len:i+1\u001b[0m\u001b[32m]\u001b[0m\u001b[32m == s\u001b[0m\u001b[32m[\u001b[0m\u001b[32mi-max_len:i+1\u001b[0m\u001b[32m]\u001b[0m\u001b[32m[\u001b[0m\u001b[32m::-1\u001b[0m\u001b[32m]\u001b[0m\u001b[32m:\\n            start = i - max_len\\n            max_len += 1\\n    return \u001b[0m\n",
       "\u001b[32ms\u001b[0m\u001b[32m[\u001b[0m\u001b[32mstart:start+max_len\u001b[0m\u001b[32m]\u001b[0m\u001b[32m'\u001b[0m\n",
       "\u001b[1m}\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "print(response.validated_output)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here's the generated code:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">def <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">longestPalindrome</span><span style=\"font-weight: bold\">(</span>s<span style=\"font-weight: bold\">)</span>:\n",
       "    if <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">len</span><span style=\"font-weight: bold\">(</span>s<span style=\"font-weight: bold\">)</span> <span style=\"font-weight: bold\">&lt;</span><span style=\"color: #000000; text-decoration-color: #000000\"> </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">2</span><span style=\"color: #000000; text-decoration-color: #000000\">:</span>\n",
       "<span style=\"color: #000000; text-decoration-color: #000000\">        return s</span>\n",
       "<span style=\"color: #000000; text-decoration-color: #000000\">    start = </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0</span>\n",
       "<span style=\"color: #000000; text-decoration-color: #000000\">    max_len = </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>\n",
       "<span style=\"color: #000000; text-decoration-color: #000000\">    for i in </span><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">range</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">(</span><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">len</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">(</span><span style=\"color: #000000; text-decoration-color: #000000\">s</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">))</span><span style=\"color: #000000; text-decoration-color: #000000\">:</span>\n",
       "<span style=\"color: #000000; text-decoration-color: #000000\">        if i - max_len &gt;= </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span><span style=\"color: #000000; text-decoration-color: #000000\"> and s == s</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">[</span><span style=\"color: #000000; text-decoration-color: #000000\">::</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">-1</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">]</span><span style=\"color: #000000; text-decoration-color: #000000\">:</span>\n",
       "<span style=\"color: #000000; text-decoration-color: #000000\">            start = i - max_len - </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>\n",
       "<span style=\"color: #000000; text-decoration-color: #000000\">            max_len += </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">2</span>\n",
       "<span style=\"color: #000000; text-decoration-color: #000000\">            continue</span>\n",
       "<span style=\"color: #000000; text-decoration-color: #000000\">        if i - max_len </span><span style=\"font-weight: bold\">&gt;</span>= <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0</span> and s == s<span style=\"font-weight: bold\">[</span>::<span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">-1</span><span style=\"font-weight: bold\">]</span>:\n",
       "            start = i - max_len\n",
       "            max_len += <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>\n",
       "    return s\n",
       "</pre>\n"
      ],
      "text/plain": [
       "def \u001b[1;35mlongestPalindrome\u001b[0m\u001b[1m(\u001b[0ms\u001b[1m)\u001b[0m:\n",
       "    if \u001b[1;35mlen\u001b[0m\u001b[1m(\u001b[0ms\u001b[1m)\u001b[0m \u001b[1m<\u001b[0m\u001b[39m \u001b[0m\u001b[1;36m2\u001b[0m\u001b[39m:\u001b[0m\n",
       "\u001b[39m        return s\u001b[0m\n",
       "\u001b[39m    start = \u001b[0m\u001b[1;36m0\u001b[0m\n",
       "\u001b[39m    max_len = \u001b[0m\u001b[1;36m1\u001b[0m\n",
       "\u001b[39m    for i in \u001b[0m\u001b[1;35mrange\u001b[0m\u001b[1;39m(\u001b[0m\u001b[1;35mlen\u001b[0m\u001b[1;39m(\u001b[0m\u001b[39ms\u001b[0m\u001b[1;39m)\u001b[0m\u001b[1;39m)\u001b[0m\u001b[39m:\u001b[0m\n",
       "\u001b[39m        if i - max_len >= \u001b[0m\u001b[1;36m1\u001b[0m\u001b[39m and s\u001b[0m\u001b[39m == s\u001b[0m\u001b[1;39m[\u001b[0m\u001b[39m::\u001b[0m\u001b[1;36m-1\u001b[0m\u001b[1;39m]\u001b[0m\u001b[39m:\u001b[0m\n",
       "\u001b[39m            start = i - max_len - \u001b[0m\u001b[1;36m1\u001b[0m\n",
       "\u001b[39m            max_len += \u001b[0m\u001b[1;36m2\u001b[0m\n",
       "\u001b[39m            continue\u001b[0m\n",
       "\u001b[39m        if i - max_len \u001b[0m\u001b[1m>\u001b[0m= \u001b[1;36m0\u001b[0m and s == s\u001b[1m[\u001b[0m::\u001b[1;36m-1\u001b[0m\u001b[1m]\u001b[0m:\n",
       "            start = i - max_len\n",
       "            max_len += \u001b[1;36m1\u001b[0m\n",
       "    return s\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "if response.validated_output is not None:\n",
    "    print(response.validated_output[\"python_code\"])\n",
    "elif response.error is not None:\n",
    "    print(response.error)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can confirm that the code is bug free by executing the code in the environment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Success!\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Success!\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "try:\n",
    "    exec(response.validated_output[\"python_code\"])\n",
    "    print(\"Success!\")\n",
    "except Exception as e:\n",
    "    print(\"Failed!\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "tiff-env",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.17"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "ef14f49bbc779f2fde64ca0552c2a99d578405052f5b73f61279551da311a8a1"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
